/*
https://leetcode.cn/problems/minimum-number-of-flips-to-make-binary-grid-palindromic-i/description/
3239. 最少翻转次数使二进制矩阵回文 I
*/

class Solution {
public:
    int minFlips(vector<vector<int>>& grid) {
        if(grid[0].size()==1) return 0;
        int n=grid.size();
        int m=grid[0].size();
        long long ans=0,num=0;
        for(int i=0;i<n;i++)
        {
            int l=0,r=m-1;
            while(l<=r)
            {
                if(grid[i][l++]!=grid[i][r--])
                {
                    ans++;
                }
            }
        }
        for(int j=0;j<m;j++)
        {
            int l=0,r=n-1;
            while(l<=r)
            {
                if(grid[l++][j]!=grid[r--][j])
                {
                    num++;
                }
            }
        }
        int t=min(ans,num);
        return t;
    }
};
